package com.rfid.project.web;

import java.text.DateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.exception.CannotDelException;
import com.rfid.project.model.ProjectMilestone;
import com.rfid.project.service.ProcessStatus;
import com.rfid.project.service.ProjectMilestoneService;
import com.rfid.project.model.Project;
import com.rfid.project.service.ProjectService;
import com.rfid.project.service.PeopleService;

@Controller
public class ProjectMilestoneController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProjectMilestoneController.class);

	@Autowired
	private ProjectMilestoneService projectMilestoneService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private PeopleService peopleService;

	@RequestMapping(value = "/projectMilestonemgr")
	public String projectMilestonemgr(HttpServletRequest request,
			HttpServletResponse response, @Valid Project project, Model model) {
		return "projectMilestonemgr";
	}

	@RequestMapping(value = "/queryProjectMilestone")
	public String queryProjectMilestone(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "projectId", defaultValue = "-1") Long projectId,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "deliveryDescription", defaultValue = "") String deliveryDescription,
			@RequestParam(value = "firstPlanFinishDateBegin", defaultValue = "") Date firstPlanFinishDateBegin,
			@RequestParam(value = "firstPlanFinishDateEnd", defaultValue = "") Date firstPlanFinishDateEnd,
			@RequestParam(value = "planFinishDateBegin", defaultValue = "") Date planFinishDateBegin,
			@RequestParam(value = "planFinishDateEnd", defaultValue = "") Date planFinishDateEnd,
			@RequestParam(value = "actualFinishDateBegin", defaultValue = "") Date actualFinishDateBegin,
			@RequestParam(value = "actualFinishDateEnd", defaultValue = "") Date actualFinishDateEnd,
			@RequestParam(value = "qualityCheckSign", defaultValue = "") String qualityCheckSign,
			@RequestParam(value = "status", defaultValue = "") String status,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<ProjectMilestone> items = projectMilestoneService
				.findAll(projectId, name, deliveryDescription,
						firstPlanFinishDateBegin, firstPlanFinishDateEnd,
						planFinishDateBegin, planFinishDateEnd,
						actualFinishDateBegin, actualFinishDateEnd,
						qualityCheckSign, status,
						remark, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", items.getTotalElements());
		JSONArray models = new JSONArray();
		for (ProjectMilestone item : items) {
			JSONObject mod = new JSONObject();
			capacityData(item, mod);

			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findProjectMilestone")
	public String findProjectMilestone(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id,
			Model model) {
		ProjectMilestone projectMilestone = projectMilestoneService.findAll(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		capacityData(projectMilestone, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/queryQualityProjectMilestone")
	public String queryQualityProjectMilestone(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "projectId", defaultValue = "-1") Long projectId,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "deliveryDescription", defaultValue = "") String deliveryDescription,
			@RequestParam(value = "firstPlanFinishDateBegin", defaultValue = "") Date firstPlanFinishDateBegin,
			@RequestParam(value = "firstPlanFinishDateEnd", defaultValue = "") Date firstPlanFinishDateEnd,
			@RequestParam(value = "planFinishDateBegin", defaultValue = "") Date planFinishDateBegin,
			@RequestParam(value = "planFinishDateEnd", defaultValue = "") Date planFinishDateEnd,
			@RequestParam(value = "actualFinishDateBegin", defaultValue = "") Date actualFinishDateBegin,
			@RequestParam(value = "actualFinishDateEnd", defaultValue = "") Date actualFinishDateEnd,
			@RequestParam(value = "qualityCheckSign", defaultValue = "") String qualityCheckSign,
			@RequestParam(value = "status", defaultValue = "") String status,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<ProjectMilestone> items = projectMilestoneService
				.findAll(projectId, name, deliveryDescription,
						firstPlanFinishDateBegin, firstPlanFinishDateEnd,
						planFinishDateBegin, planFinishDateEnd,
						actualFinishDateBegin, actualFinishDateEnd,
						qualityCheckSign, status,
						remark, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", items.getTotalElements());
		JSONArray models = new JSONArray();
		for (ProjectMilestone item : items) {
			JSONObject mod = new JSONObject();
			capacityData(item, mod);
			
			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findQualityProjectMilestone")
	public String findQualityProjectMilestone(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id,
			Model model) {
		ProjectMilestone projectMilestone = projectMilestoneService.findOne(id);
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		capacityData(projectMilestone, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}
	private void capacityData(ProjectMilestone item, JSONObject mod) {
		mod.put("id", item.getId());
		mod.put("name", item.getName());
		mod.put("firstPlanFinishDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getFirstPlanFinishDate()));
		mod.put("planFinishDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getPlanFinishDate()));
		mod.put("planReviseCount", item.getPlanReviseCount());
		mod.put("actualFinishDate", com.rfid.project.util.DateUtil.dateTheDayToString(item.getActualFinishDate()));
		
		mod.put("qualityCheckSign", item.getQualityCheckSign());
		mod.put("status", ProcessStatus.processStatusName(item.getStatus()));
		mod.put("statusCode", item.getStatus());
		
		String description = item.getDeliveryDescription();
		mod.put("deliveryDescription", description == null?"":description);
		
		String content = item.getRemark();
		mod.put("remark", content == null ? "" : content);
		
		mod.put("projectId", item.getProject().getId());
		mod.put("projectName", item.getProject().getName());
	}

	@RequestMapping(value = "/editProjectMilestone", method = RequestMethod.POST)
	public String editProjectMilestone(HttpServletRequest request,
			HttpServletResponse response, @Valid ProjectMilestone projectMilestone,
			Model model, RedirectAttributes redirectAttributes) {
		projectMilestoneService.saveProjectMilestone(projectMilestone);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@RequestMapping(value = "/editProjectMilestoneQualityCheck", method = RequestMethod.POST)
	public String editProjectMilestoneQualityCheck(HttpServletRequest request,
			HttpServletResponse response, @Valid ProjectMilestone projectMilestone,
			Model model, RedirectAttributes redirectAttributes) {
		projectMilestoneService.editProjectMilestoneQualityCheck(projectMilestone);
		
		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}
	@RequestMapping(value = "/pullDownProjectMilestone", method = RequestMethod.POST)
	public String pullDownProjectMilestone(HttpServletRequest request,
			HttpServletResponse response, @Valid ProjectMilestone projectMilestone,
			Model model, RedirectAttributes redirectAttributes) {
		projectMilestoneService.pullDownProjectMilestone(projectMilestone);
		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delProjectMilestone")
	public String delProjectMilestone(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		try {
			this.projectMilestoneService.deleteProjectMilestone(id);
		} catch (CannotDelException e) {
			flag = false;
			result.put("msg", e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}
	
	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
